package com.hex.liyue.nearmodule.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hex.liyue.LiYueApplication;
import com.hex.liyue.R;
import com.hex.liyue.commonmodule.glide.GlideCircleTransform;
import com.hex.liyue.commonmodule.utils.DateUtils;
import com.hex.liyue.commonmodule.utils.Distance;
import com.hex.liyue.nearmodule.bean.NearListBean;

import java.util.List;

/**
 * Created by yukoyuan on 16/7/11.
 * 附近列表的适配器
 */
public class NearListAdapter extends RecyclerView.Adapter<NearListAdapter.MyHolder> {
    List<NearListBean.DataBean> nearlist;
    private OnItemClickListener onItemClickListener;
    private String gpsx;
    private String gpsy;

    public NearListAdapter(List<NearListBean.DataBean> nearlist) {
        this.nearlist = nearlist;
    }

    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(parent.getContext(), R.layout.item_nearlist, null);
        return new MyHolder(view);
    }

    public void setLoaction(String gpsx, String gpsy) {
        this.gpsx = gpsx;
        this.gpsy = gpsy;
    }

    @Override
    public void onBindViewHolder(MyHolder holder, final int position) {
        NearListBean.DataBean dataBean = nearlist.get(position);
        Glide.with(LiYueApplication.getInstance())
                .load(dataBean.launch_user.avatar)
                .error(R.drawable.default_error)
                .transform(new GlideCircleTransform(LiYueApplication.getInstance()))
                .into(holder.iv_avater);
        if (!"".equals(dataBean.launch_user.birthday)) {
            long birthday = DateUtils.getAge(Long.valueOf(dataBean.launch_user.birthday) * 1000);
            double distance = Distance.getDistance(Double.parseDouble(gpsx), Double.parseDouble(gpsy), Double.parseDouble(dataBean.gps_x), Double.parseDouble(dataBean.gps_y));
            if (birthday == 0) {
                if (distance > 1000) {
                    holder.tv_name.setText(dataBean.launch_user.nickname + "(" + (int)Math.rint(distance / 1000) + "km)");
                }else{
                    if(distance<100){
                        holder.tv_name.setText(dataBean.launch_user.nickname + "(" + 100 + "m以内)");
                    }else if(distance>100&&distance<200){
                        holder.tv_name.setText(dataBean.launch_user.nickname + "(" + 200 + "m以内)");
                    }else if(distance>200&&distance<300){
                        holder.tv_name.setText(dataBean.launch_user.nickname + "(" + 300 + "m以内)");
                    }else if(distance>300&&distance<400){
                        holder.tv_name.setText(dataBean.launch_user.nickname + "(" + 400 + "m以内)");
                    }else if(distance>400&&distance<500){
                        holder.tv_name.setText(dataBean.launch_user.nickname + "(" + 500 + "m以内)");
                    }else if(distance>500&&distance<600){
                        holder.tv_name.setText(dataBean.launch_user.nickname + "(" + 600 + "m以内)");
                    }else if(distance>600&&distance<700){
                        holder.tv_name.setText(dataBean.launch_user.nickname + "(" + 700 + "m以内)");
                    }else if(distance>700&&distance<800){
                        holder.tv_name.setText(dataBean.launch_user.nickname + "(" + 800 + "m以内)");
                    }else{
                        holder.tv_name.setText(dataBean.launch_user.nickname + "(" + 900 + "m以内)");
                    }
                }
            } else {
                if (distance > 1000) {
                    holder.tv_name.setText(dataBean.launch_user.nickname + "   " + birthday + "  " + "(" +(int) Math.rint(distance / 1000)+ "km)");
                }else{
                    if(distance<100){
                        holder.tv_name.setText(dataBean.launch_user.nickname  + "   " + birthday + " (" + 100 + "m以内)");
                    }else if(distance>100&&distance<200){
                        holder.tv_name.setText(dataBean.launch_user.nickname  + "   " + birthday + " (" + 200 + "m以内)");
                    }else if(distance>200&&distance<300){
                        holder.tv_name.setText(dataBean.launch_user.nickname  + "   " + birthday + " (" + 300 + "m以内)");
                    }else if(distance>300&&distance<400){
                        holder.tv_name.setText(dataBean.launch_user.nickname  + "   " + birthday + " (" + 400 + "m以内)");
                    }else if(distance>400&&distance<500){
                        holder.tv_name.setText(dataBean.launch_user.nickname  + "   " + birthday +" (" + 500 + "m以内)");
                    }else if(distance>500&&distance<600){
                        holder.tv_name.setText(dataBean.launch_user.nickname  + "   " + birthday + " (" + 600 + "m以内)");
                    }else if(distance>600&&distance<700){
                        holder.tv_name.setText(dataBean.launch_user.nickname  + "   " + birthday + " (" + 700 + "m以内)");
                    }else if(distance>700&&distance<800){
                        holder.tv_name.setText(dataBean.launch_user.nickname  + "   " + birthday + " (" + 800 + "m以内)");
                    }else{
                        holder.tv_name.setText(dataBean.launch_user.nickname  + "   " + birthday + " (" + 900 + "m以内)");
                    }
                }
            }
        }

        holder.tv_begin_date.setText(DateUtils.getFormatDate(Long.valueOf(dataBean.invitations_time) * 1000, "yyyy年MM月dd日 HH:mm"));
        holder.tv_location.setText(dataBean.location);
        holder.tv_event.setText(dataBean.event);
        holder.tv_number.setText(dataBean.invitations_real_number + "人约");
        holder.tv_gift_total.setText("收到￥" + dataBean.gift_total);
        holder.rl_item_nearlist.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClickListener(nearlist.get(position).appointment_id);
                }
            }
        });
    }

    public interface OnItemClickListener {
        void onItemClickListener(String appointment_id);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }


    @Override
    public int getItemCount() {
        return nearlist.size();
    }

    class MyHolder extends RecyclerView.ViewHolder {
        ImageView iv_avater;
        TextView tv_name, tv_begin_date, tv_location, tv_event, tv_number, tv_gift_total;
        RelativeLayout rl_item_nearlist;

        public MyHolder(View itemView) {
            super(itemView);
            rl_item_nearlist = (RelativeLayout) itemView.findViewById(R.id.rl_item_nearlist);
            iv_avater = (ImageView) itemView.findViewById(R.id.iv_avater);
            tv_name = (TextView) itemView.findViewById(R.id.tv_name);
            tv_begin_date = (TextView) itemView.findViewById(R.id.tv_begin_date);
            tv_location = (TextView) itemView.findViewById(R.id.tv_location);
            tv_event = (TextView) itemView.findViewById(R.id.tv_event);
            tv_number = (TextView) itemView.findViewById(R.id.tv_number);
            tv_gift_total = (TextView) itemView.findViewById(R.id.tv_gift_total);
        }
    }
}
